package com.ww.grtsmanager.service.impl;

import com.ww.grtsmanager.bean.Video;
import com.ww.grtsmanager.bean.VideoStatus;
import com.ww.grtsmanager.vo.VideoResult;
import com.ww.grtsmanager.mapper.VideoMapper;
import com.ww.grtsmanager.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public void deleteVideo(Integer id) {
        int i = videoMapper.deleteVideo(id);
        if (i == 0) {
            throw new RuntimeException("删除小节视频失败");
        }

    }

    @Override
    public Video videoUpload(MultipartFile file) {
        Video video = new Video();
        long size = file.getSize();
        double d = size / 1024;
        d = d / 1024;
        video.setSize(String.format("%.2f", d));
        String originalFilename = file.getOriginalFilename();
        video.setVideoOriginalName(originalFilename);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        File folder = new File("D:\\pic", date);
        if (!folder.exists()) {
            folder.mkdir();//创建文件
        }
        //改变后的全新名称
        String title = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        video.setTitle(title);
        String videoSourceId = "http://localhost/" + date + "/" + title;
        video.setVideoSourceId(videoSourceId);
        File f = new File(folder, title);
        try {
            //把图片保存到电脑D:\\pic\当前日期\\随机生成的名称.mp4下面去
            file.transferTo(f);
            MultimediaObject multimediaObject = new MultimediaObject(f);
            MultimediaInfo result = multimediaObject.getInfo();
            long duration = result.getDuration() / 1000L;
            video.setDuration(duration);
        } catch (IOException e) {
            throw new RuntimeException("上传视频失败");
        } catch (InputFormatException e) {
            throw new RuntimeException("上传视频失败");
        } catch (EncoderException e) {
            throw new RuntimeException("上传视频失败");
        }
        return video;
    }

    @Override
    public VideoResult getVideo(Integer id) {
        VideoResult result = videoMapper.getVideo(id);
        return result;
    }

    @Override
    public void addVideo(Video video) {
        video.setPlayCount(0L);
        video.setStatus(VideoStatus.Normal);
        Date date = new Date();
        video.setGmtCreated(date);
        video.setGmtModified(date);
        int i = videoMapper.addVideo(video);
        if (i==0){
            throw new RuntimeException("添加小节失败");
        }
    }
}
